#include "include/test1.h"


status ListInit(sqlist* list)
{
    int i;
    for(i=0;i<MaxNum;i++){
        list->data[i] = 0;
    }
    list->last = 0;
    return TURE;
}

//  i表示查找的是第i个 并不是从0开始
status GetElem(sqlist *list, int i, data_type *data)
{
    if(list->last==0 || i<1 || i>list->last)
        return ERROR;
    *data = list->data[i-1];
    return TURE;
}

status ListInsert(sqlist *list, int i, data_type data)
{
    int j;
    if( list->last>MaxNum )     // 表满
        return ERROR;
    if( i<1 || i>list->last+1)  // 要插入的位置大于当前表的大小 比如表有10个  那只能放在11位  不能放在12位
        return ERROR;
    if( i<list->last ){
        for(j=list->last-1; j>i; j--)
            list->data[j+1] = list->data[j];
    }
    list->data[i-1] = data;
    list->last++;
    return TURE;
}

status ListDelete(sqlist *list, int i)
{
    int j;
    if( i > list->last || i < 1 )
        return ERROR;
    if( i < list->last ){
        list->data[i-1] = list->data[i];
    }
    list->data[i] = 0;
    list->last--;
}
// 线性表的释放
status ListFree(sqlist *list)
{
    if( list->last < 1 )
        return ERROR;
    free(list);
}

void test1(void)
{
    status ret;
    data_type data;
    sqlist L;
    ret = ListInit(&L);
    if( ret != ERROR )
        printf("sqlist init TURE\n ");
    else
        printf("sqlist init ERROR\n");
    data = 1;
    ret = ListInsert(&L, 1, data);
    if( ret != ERROR )
        printf("sqlist Insert TURE\n ");
    else
        printf("sqlist Insert ERROR\n");
    data = 3;
    ret = ListInsert(&L, 2, data);
    if( ret != ERROR )
        printf("sqlist Insert TURE\n ");
    else
        printf("sqlist Insert ERROR\n");
    ret = GetElem(&L, 2, &data);
    if( ret != ERROR )
        printf("From sqlist read date = %d\n ",data);
    else
        printf("From sqlist read error\n");
    
    for(int i = 0; i < L.last; i++){
        printf("i=%d   %d\n",i,L.data[i]);
    }
    ListDelete(&L, 2);
    for(int i = 0; i < L.last; i++){
        printf("i=%d   %d\n",i,L.data[i]);
    }
}

/* 遍历顺序表 */ 
status ListAllprint(sqlist *L)
{
    if( L->last < 1 ){
        printf("error empty\n");
        return ERROR;
    }
    for( int i = 1; i < L->last; i++){
        printf("i=%d %d\n",i,L->data[i-1]);
    }
    return TURE;
}

